package com.smarttest.portal.controller;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarttest.common.beans.PaginationResult;
import com.smarttest.portal.pojo.Checkinterface;
import com.smarttest.portal.service.CheckInterfaceService;
import com.smarttest.common.beans.StatResult;

@Controller
public class CheckInterfaceController {
	@Autowired
	CheckInterfaceService service;

	@RequestMapping(value = "/checkInterface/{page}pg&{rows}row.action", method = RequestMethod.GET)
	@ResponseBody
	public PaginationResult getItemList(@PathVariable Integer page,
			@PathVariable Integer rows) {
		PaginationResult result = service.getCheckinterList(page, rows);
		return result;
	}

	@RequestMapping(value = "/checkInterface/getTitle.action", method = RequestMethod.GET)
	@ResponseBody
	public List<String> getTitle(HttpServletRequest request,
			HttpServletResponse response) {
		ArrayList<String> list = new ArrayList<String>();
		Field[] fields = Checkinterface.class.getDeclaredFields();
		for (Field field : fields) {
			list.add(field.getName());
		}
		return list;
	}

	@RequestMapping(value = "/checkInterface/searchBy.action", method = RequestMethod.GET)
	@ResponseBody
	public PaginationResult getItemListByTime(HttpServletRequest request,
			HttpServletResponse response) {
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String orderBy = request.getParameter("orderBy");
		String pageStr = request.getParameter("page");
		String rowsStr = request.getParameter("rows");
		System.out
				.println("---------pageStr:" + pageStr + "rowsStr:" + rowsStr);
		int page = Integer.parseInt(pageStr);
		int rows = Integer.parseInt(rowsStr);

		Date start = null;
		Date end = null;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			start = sdf.parse(startTime);
			end = sdf.parse(endTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		PaginationResult result = service.getCheckinterListByTime(start, end,
				orderBy, page, rows);

		return result;
	}
	
	@RequestMapping(value = "/checkInterface/getResult.action",method = RequestMethod.GET)
	@ResponseBody
	public StatResult getStatResult(HttpServletRequest request, HttpServletResponse response) {
		String startTime = request.getParameter("starttime");
		String endTime = request.getParameter("endtime");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start = null;
		Date end = null;
		
		startTime = startTime+" 00:00:00";
		endTime = endTime+" 23:59:59";
		
		try {
			start = sdf.parse(startTime);
		 } catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		 }
		 try {
			end = sdf.parse(endTime);
		 } catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		 }
		
		StatResult result = service.getStatResult(start, end);
		return result;
	}
}
